package com.ironman.analyze.core.compute;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.ironman.analyze.core.compute.base.BaseAverageComputer;
import com.ironman.analyze.core.compute.base.BaseComputer;
import com.ironman.analyze.core.compute.entity.HeartrateWindowData;
import com.ironman.analyze.core.context.TrainingContext;
import com.ironman.analyze.core.handler.HeartratePostHandler;
import com.ironman.common.entity.HeartrateData;
import com.ironman.common.params.HeartrateParam;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * 心率计算
 */
@Slf4j
public class Heartrate2AverageComputer extends BaseAverageComputer<HeartrateData> implements BaseComputer<HeartrateData, HeartrateWindowData> {

    private final HeartratePostHandler heartratePostHandler;

    public Heartrate2AverageComputer(TrainingContext trainingContext, HeartrateParam heartrateParam) {
        super(heartrateParam.getWindowSize(), trainingContext);
        this.heartratePostHandler = new HeartratePostHandler(heartrateParam);
    }

    @Override
    public void computeWindowData(List<HeartrateData> dataList) {
        for (HeartrateData heartrateData : dataList) {
            windowData(heartrateData, new BiConsumer<Date, Double>() {
                @Override
                public void accept(Date ts, Double heading) {
                    HeartrateWindowData heartrateWindowData = new HeartrateWindowData(ts, heading);
                    heartratePostHandler.handler(heartrateWindowData, new Consumer<HeartrateWindowData>() {
                        @Override
                        public void accept(HeartrateWindowData heartrateWindowData) {
                            syncData(heartrateWindowData);
                        }
                    });
                }
            });
        }
    }

    @Override
    public void syncData(HeartrateWindowData windowData) {
        getTrainingContext().getHeartrateWindowData2Map().put(windowData.getTs(), windowData);
    }


    public static void main(String[] args) {
        Heartrate2AverageComputer heartbeatComputer = new Heartrate2AverageComputer(null, null);

        List<HeartrateData> heartrateDataList = ListUtil.of(
                new HeartrateData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 112),
                new HeartrateData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 120),
                new HeartrateData(DateUtil.parse("2024-12-15 12:00:02", DatePattern.NORM_DATETIME_PATTERN), 148),
                new HeartrateData(DateUtil.parse("2024-12-15 12:00:03", DatePattern.NORM_DATETIME_PATTERN), 268),
                new HeartrateData(DateUtil.parse("2024-12-15 12:00:04", DatePattern.NORM_DATETIME_PATTERN), 59),
                new HeartrateData(DateUtil.parse("2024-12-15 12:00:05", DatePattern.NORM_DATETIME_PATTERN), 67),
                new HeartrateData(DateUtil.parse("2024-12-15 12:00:06", DatePattern.NORM_DATETIME_PATTERN), 11),
                new HeartrateData(DateUtil.parse("2024-12-15 12:00:07", DatePattern.NORM_DATETIME_PATTERN), 114),
                new HeartrateData(DateUtil.parse("2024-12-15 12:00:08", DatePattern.NORM_DATETIME_PATTERN), 88),
                new HeartrateData(DateUtil.parse("2024-12-15 12:00:09", DatePattern.NORM_DATETIME_PATTERN), 100)
        );

        heartbeatComputer.computeWindowData(heartrateDataList);
    }
}
